Skip to content

Combine the values of a block with sub-group reductions - #205

Merged
maleadt merged 1 commit into
tb/reduce-localmemfrom
tb/reduce-subgroups
Nov 8, 2026
Merged

maleadt merged 1 commit into
tb/reduce-localmemfrom
tb/reduce-subgroups

Conversation

@maleadt

@maleadt maleadt commented Nov 5, 2026

Copy link
Copy Markdown
Member

The reduction kernels combine a block's values in local memory, one per thread, with a tree of log2(block size) synchronized steps. Where the backend has linear sub-groups that can shuffle the accumulator, they now reduce each sub-group's values with KI.sub_group_reduce, and then the sub-groups' values, as CUB's BLOCK_REDUCE_WARP_REDUCTIONS. A block keeps one value per sub-group in local memory instead of one per thread.

  • Whole-array, by-block, multigroup and second-pass kernels: block_reduce, which also replaces the block scan of the ordered kernel, as the sub-group reductions combine in lane order.
  • ByLanes: lanes that fit a sub-group reduce with sub_group_reduce(op, x, lanes) and no local memory.
  • Tiled and columns kernels keep their trees: their lanes span several sub-groups.
  • A new tuning flag subgroups turns this off, as for the scans; cpu_reduce_tuning does, since CPUs run sub-group operations as block barriers. A test forces it on for the host backend, so that PoCL runs the sub-group paths in CI.

Kernel times on an RTX 5080, interleaving subgroups=false and true (median of 6 runs, each the best of 100), are unchanged within noise:

off on
sum Float32, 10^8 446.6 µs 446.2 µs
sum Float64, 10^7 98.0 µs 97.8 µs
max Float32, 10^8 445.6 µs 444.9 µs
sum Float32 4096², dims=1 30.9 µs 30.9 µs
sum Float32 256×10^5, dims=1 118.1 µs 119.4 µs
sum Float32 10^6×8, dims=1 24.4 µs 23.1 µs
sum Float32 4096², dims=2 47.6 µs 47.3 µs
ordered sum Float32, 10^7 18.9 µs 19.1 µs
sum of 136-byte structs, 10^6 192.2 µs 193.0 µs
sum of 136-byte structs 512×2000, dims=1 185.6 µs 182.3 µs

Structs of 8 to 64 bytes over 15 runs and ComplexF64 sums are within 1%. So this mostly frees local memory, which matters for wide accumulators; it doesn't speed up the memory-bound reductions measured here.

The reduction kernels combined a block's values in local memory, one per thread, with a tree of
log2(block size) synchronized steps. Where the backend has linear sub-groups that can shuffle
the accumulator, reduce each sub-group's values with `KI.sub_group_reduce` and then the
sub-groups' values, as CUB's `BLOCK_REDUCE_WARP_REDUCTIONS`, keeping one value per sub-group in
local memory. This serves the whole-array, by-block, multigroup and second-pass kernels, and the
ordered kernel, as the sub-group reductions combine in lane order; `ByLanes` reductions whose
lanes fit a sub-group use no local memory at all. The strided kernels, whose lanes span several
sub-groups, keep their trees.

The tuning's new `subgroups` turns this off, as for the scans on CPU devices. On an RTX 5080
the kernel times are unchanged within noise (Float32 to ComplexF64 sums and maxima, reductions
along `dims` of several shapes, in element order, and of structs of 8 to 136 bytes), so this
mainly frees local memory.
@maleadt
maleadt force-pushed the tb/reduce-subgroups branch from 653ccd5 to a0f8018 Compare November 6, 2026 09:59
@maleadt
maleadt merged commit ac29f5b into main Nov 8, 2026
17 checks passed
@maleadt
maleadt deleted the tb/reduce-subgroups branch November 8, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant